Imports System.IO

Public Class WorkWithDirectory
  Private Enum ItemType
    Drive
    Folder
    File
  End Enum

  Private Sub WorkWithDirectory_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    CreateDirectoryTextBox.Text = _
      Directory.GetCurrentDirectory()

        For Each drive As DriveInfo In DriveInfo.GetDrives()
            Dim node As TreeNode = _
             FilesTreeView.Nodes.Add(drive.Name)
            node.Tag = ItemType.Drive
            ' Add a node so you see a +.
            node.Nodes.Add("FILLER")
        Next
  End Sub

  Private Sub CreateButton_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles CreateButton.Click

    Try
      Directory.CreateDirectory(CreateDirectoryTextBox.Text)
      ' Copy the text for testing deletion.
      DeleteTextBox.Text = CreateDirectoryTextBox.Text
            Dim di As DirectoryInfo = New DirectoryInfo("C:\a")

    Catch ex As Exception
      MessageBox.Show(ex.Message, "Unable to Create Directory")
    End Try
  End Sub

  Private Sub DeleteButton_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles DeleteButton.Click

    Dim doDeleteFiles As Boolean = _
      (MessageBox.Show( _
      "Delete files contained within the folder?", _
      "Delete Folder", _
      MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes)

    Try
      Directory.Delete(DeleteTextBox.Text, doDeleteFiles)

    Catch ex As Exception
      MessageBox.Show(ex.Message, "Unable to Delete Directory")
    End Try
  End Sub

  Private Sub FilesTreeView_BeforeExpand( _
    ByVal sender As Object, _
    ByVal e As TreeViewCancelEventArgs) _
    Handles FilesTreeView.BeforeExpand

    Dim currentNode As TreeNode = e.Node
    currentNode.Nodes.Clear()

    Try
      ' Now go get all the files and folders.
      Dim fullPathString As String = currentNode.FullPath

      ' Handle each folder.
      For Each folderString As String In _
       Directory.GetDirectories(fullPathString)
        Dim newNode As TreeNode = _
         currentNode.Nodes.Add(Path.GetFileName(folderString))
        newNode.Tag = ItemType.Folder
        newNode.Nodes.Add("FILLER")
      Next

      ' Handle each file.
      For Each fileString As String In _
       Directory.GetFiles(fullPathString)
        ' Get just the file name portion (without the path):
        Dim newNode As TreeNode = _
         currentNode.Nodes.Add(Path.GetFileName(fileString))
        newNode.Tag = ItemType.File
      Next

    Catch ex As Exception
      MessageBox.Show(ex.Message, _
       "Error Displaying Information")
    End Try
  End Sub

  Private Sub FilesTreeView_AfterSelect( _
    ByVal sender As System.Object, _
    ByVal e As TreeViewEventArgs) _
    Handles FilesTreeView.AfterSelect

    Dim item As ItemType = CType(e.Node.Tag, ItemType)
    HandlePanels(item)

    Select Case item
      Case ItemType.Drive
        DisplayDriveInfoProperties(e.Node.Text)

      Case ItemType.File
        ' FileInfo objects don't supply 
        ' Parent or Root properties:
        ParentLabel.Text = String.Empty
        RootLabel.Text = String.Empty

        Dim fi As New FileInfo(e.Node.FullPath)
        LengthLabel.Text = fi.Length.ToString
        DisplayCommonProperties(fi)

      Case ItemType.Folder
        ' DirectoryInfo objects don't provide
        ' a Length property:
        LengthLabel.Text = String.Empty

        Dim di As New DirectoryInfo(e.Node.FullPath)
        ParentLabel.Text = di.Parent.Name
        RootLabel.Text = di.Root.Name
        DisplayCommonProperties(di)
    End Select
  End Sub

  Private Sub HandlePanels(ByVal item As ItemType)
    Select Case item
      Case ItemType.Drive
        DriveInfoPanel.Visible = True
        FileSystemInfoPanel.Visible = False
      Case ItemType.File
        DriveInfoPanel.Visible = False
        FileSystemInfoPanel.Visible = True
      Case ItemType.Folder
        DriveInfoPanel.Visible = False
        FileSystemInfoPanel.Visible = True
    End Select
  End Sub

  Private Sub DisplayCommonProperties( _
   ByVal fsi As FileSystemInfo)

    ' Display proeprties common to both DirectoryInfo
    ' and FileInfo objects.
    AttributesLabel.Text = fsi.Attributes.ToString
    CreationTimeLabel.Text = fsi.CreationTime.ToString
    LastAccessTimeLabel.Text = fsi.LastAccessTime.ToString
    LastWriteTimeLabel.Text = fsi.LastWriteTime.ToString
    ExtensionLabel.Text = fsi.Extension
    FullNameLabel.Text = fsi.FullName
    NameLabel.Text = fsi.Name
  End Sub

  Private Sub DisplayDriveInfoProperties(ByVal driveName As String)
    ' Display DriveInfo properties:
    NameTextBox.Text = driveName
    Dim drvInfo As New DriveInfo(driveName)
    DriveTypeTextBox.Text = drvInfo.DriveType.ToString()
    IsReadyTextBox.Text = drvInfo.IsReady.ToString()
    If drvInfo.IsReady Then
      AvailableFreeSpaceTextBox.Text = drvInfo.AvailableFreeSpace.ToString("N0")
      DriveFormatTextBox.Text = drvInfo.DriveFormat
      RootDirectoryTextBox.Text = drvInfo.RootDirectory.Name
      TotalFreeSpaceTextBox.Text = drvInfo.TotalFreeSpace.ToString("N0")
      TotalSizeTextBox.Text = drvInfo.TotalSize.ToString("N0")
      VolumeLabelTextBox.Text = drvInfo.VolumeLabel
    Else
      AvailableFreeSpaceTextBox.Text = String.Empty
      DriveFormatTextBox.Text = String.Empty
      RootDirectoryTextBox.Text = String.Empty
      TotalFreeSpaceTextBox.Text = String.Empty
      TotalSizeTextBox.Text = String.Empty
      VolumeLabelTextBox.Text = String.Empty
    End If
  End Sub
End Class
